package com.hogan.hoganproject.base.activity;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.hogan.hoganproject.base.utils.CheckIsNullUtils;

import java.util.Stack;

/**
 * Author: chenhu
 * Time: 2020/8/28 15:27
 * Description do somethings
 */
public class ActivityUtil {
    private static Stack<AppCompatActivity> activityStack = new Stack<>();

    public static void addActivity(AppCompatActivity activity) {
        if (activityStack == null) {
            activityStack = new Stack<>();
        }
        activityStack.add(activity);
    }

    /**
     * 结束所有的Activity
     */
    public static void finishAllActivity() {
        int size = activityStack.size();
        for (int i = 0; i < size; i++) {
            if (null != activityStack.get(i)) {
                activityStack.get(i).finish();
            }
        }
        activityStack.clear();
    }

    /**
     * 杀掉程序并退出
     */
    public static void exitApp(Context context) {
        try {
            finishAllActivity();
            ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            activityManager.restartPackage(context.getPackageName());
            System.exit(0);
        } catch (Exception e) {
            e.fillInStackTrace();
        }
    }

    public static void addFragmentToActivity(@NonNull FragmentManager fragmentManager,
                                             @NonNull Fragment fragment, int frameId, String tag) {
        addFragmentToActivity(fragmentManager, fragment, frameId, tag, 0, 0);
    }

    public static void replaceFragmentToActivity(@NonNull FragmentManager fragmentManager,
                                                 @NonNull Fragment fragment, int frameId, String tag) {

        replaceFragmentToActivity(fragmentManager, fragment, frameId, tag, 0, 0);
    }

    private static void addFragmentToActivity(@NonNull FragmentManager fragmentManager,
                                              @NonNull Fragment fragment, int frameId, String tag, int animStar, int animEnd) {

        CheckIsNullUtils.checkIsNotNull(fragmentManager);
        CheckIsNullUtils.checkIsNotNull(fragment);
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(frameId, fragment, tag)
                .commitAllowingStateLoss();
    }

    private static void replaceFragmentToActivity(@NonNull FragmentManager fragmentManager,
                                                  @NonNull Fragment fragment, int frameId, String tag, int animStar, int animEnd) {
        CheckIsNullUtils.checkIsNotNull(fragmentManager);
        CheckIsNullUtils.checkIsNotNull(fragment);
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(frameId, fragment, tag)
                .commitAllowingStateLoss();
    }

    /**
     * Activity 跳转
     *
     * @param context
     * @param activity
     */
    public static void skipActivity(Context context, Class<?> activity) {
        Intent intent = new Intent(context, activity);
        context.startActivity(intent);
    }

    /**
     * Activity 跳转
     *
     * @param context
     * @param goal
     */
    public static void skipActivity(Context context, Class<?> goal, Bundle bundle) {
        Intent intent = new Intent(context, goal);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    /**
     * Activity 跳转后结束当前
     *
     * @param context
     * @param activity
     */
    public static void skipActivityAndFinish(Context context, Class<?> activity) {
        Intent intent = new Intent(context, activity);
        context.startActivity(intent);
        finishActivity(activity);
    }

    /**
     * Activity 跳转后结束当前
     *
     * @param context
     * @param activty
     */
    public static void skipActivityAFinish(Context context, Class<?> activty, Bundle bundle) {
        Intent intent = new Intent(context, activty);
        intent.putExtras(bundle);
        context.startActivity(intent);
        finishActivity(activty);
    }

    /**
     * 结束指定类名的Activity
     */
    public static void finishActivity(Class<?> cls) {
        for (AppCompatActivity activity : activityStack) {
            if (activity.getClass().equals(cls)) {
                finishActivity(activity);
            }
        }
    }

    /**
     * 结束指定的Activity
     */
    public static void finishActivity(AppCompatActivity activity) {

        if (activity != null) {
            activityStack.remove(activity);
            activity.finish();
        }
    }
}
